<template>
    <div id="board">
        <div class="mod_focus">
            <div class="focus_bg" :style="{backgroundImage: 'url(' + albumpic_big + ')'}"></div>
            <div class="focus_body">
                <div class="focus_info">
                    <div class="focus_text">
                        <h1>{{$route.params.topname}}</h1>
                        <p>{{update_time}} 更新</p>
                    </div>
                    <router-link :to="{name: 'playsong', params: {'playIndex': 0, 'songIds': songIds.join(',')}}"></router-link>
                </div>
			</div>
            <div class="focus_gradient" :style="{'color': bgcolor}"></div>
        </div>
        <ul class="mod_song_list u-border-t" :style="{'background': bgcolor}">
            <li v-for="(item, index) in toplist" :data-songid="item.songid" :data-no="index">
                <a href="javascript:;">
                    <div class="song_list_no">{{index + 1}}</div>
                    <div class="song_list_body u-border-b">
                        <h3>{{item.songname}}</h3>
                        <p>{{item.singername}}</p>
                    </div>
                </a>
            </li>
        </ul>
    </div>
</template>

<script>
    import { Indicator, MessageBox } from 'mint-ui';
    export default {
        name: 'board',
        data () {
            return {
                update_time: '',    // 榜单更新时间
                albumpic_big: '',   // 榜单榜首歌曲的图片
                bgcolor: '',        // 背景颜色
                toplist: [],        // 榜单歌曲列表
                songIds: []          // 榜单歌曲前10首 歌曲id 集合
            }
        },
        mounted () {
            var topid = this.$route.params.topid;
            var that = this;

            if (localStorage.getItem('toplist' + topid)) {
                var obj = JSON.parse(localStorage.getItem('toplist' + topid));

                that.toplist = obj.toplist;
                that.update_time = obj.update_time;
                that.albumpic_big = obj.albumpic_big;
                that.bgcolor = obj.bgcolor;
                that.songIds = obj.songIds;
                return;
            }

            Indicator.open({
                text: '加载中...',
                spinnerType: 'fading-circle'
            });

            this.$http.get('http://route.showapi.com/213-4', {
                params: {
                    showapi_appid: 26444,
                    showapi_sign: 'e6ed68d43d734b78892a649fedd90cbe',
                    topid: topid
                }
            })
            .then(function(res) {
                var data = res.data;
                if (data && data.showapi_res_code == 0) {
                    // 榜单歌曲列表
                    that.toplist = data.showapi_res_body.pagebean.songlist;
                    // 榜单更新时间
                    that.update_time = data.showapi_res_body.pagebean.update_time;
                    // 榜单榜首歌曲图片
                    that.albumpic_big = data.showapi_res_body.pagebean.songlist[0].albumpic_big;
                    // 背景颜色
                    var color = '' + data.showapi_res_body.pagebean.color;
                    color = color.length >= 7 ? color : '000000';
                    that.bgcolor = 'rgb(' + color.substr(0, 2) + ', ' + color.substr(2, 2) + ', ' + color.substr(4, 2) + ')';
                    
                    // 获取前10首歌曲的歌曲id
                    for (var i = 0; i < 10; i++) {
                        that.songIds.push(that.toplist[i].songid);
                    }
                    
                    var obj = {
                        toplist: that.toplist,
                        update_time: that.update_time,
                        albumpic_big: that.albumpic_big,
                        bgcolor: that.bgcolor,
                        songIds: that.songIds
                    }

                    localStorage.setItem('toplist' + topid, JSON.stringify(obj));
                } else {
                    MessageBox.alert('网络连接超时，请稍后重试！')
                    .then(function() {
                        location.reload();
                    })
                }
                Indicator.close();
            })
        }
    }
</script>

<style lang="scss">
    #board {
        .mod_focus {
            position: relative;
            overflow: hidden;

            .focus_bg {
                background-repeat: no-repeat;
                background-position: top center;
                background-size: cover;

                &:after {
                    content: '';
                    display: block;
                    padding-top: 100%;
                }
            }

            .focus_body {
                position: absolute;
                bottom: 0;
                left: 0;
                right: 0;
                z-index: 3;

                .focus_info {
                    display: flex;
                    justify-content: center;
                    margin: 0 15px 25px;

                    .focus_text {
                        flex: 1;
                        color: #fff;

                        h1 {
                            height: 36px;
                            font-size: 24px;
                            font-weight: 400px;
                            overflow: hidden;
                            white-space: nowrap;
                            text-overflow: ellipsis;
                        }

                        p {
                            overflow: hidden;
                            white-space: nowrap;
                            text-overflow: ellipsis;
                            font-size: 14px;
                        }
                    }

                    a {
                        position: relative;
                        display: block;
                        width: 42px;
                        height: 42px;
                        margin-left: 10px;
                        border: 3px solid #fff;
                        border-radius: 50%;

                        &:before {
                            content: '';
                            position: absolute;
                            border-width: 9px 15px;
                            border-style: solid;
                            border-color: transparent transparent transparent #fff;
                            top: 12px;
                            left: 14px;
                        }
                    }
                }
            }

            .focus_gradient {
                color: #000;
                position: absolute;
                bottom: 0;
                left: 0;
                z-index: 2;
                width: 100%;
                background-image: -webkit-linear-gradient(top, transparent, currentColor 80%);

                &:after {
                    content: '';
                    display: block;
                    padding-top: 60%;
                }
            }
        }
        .mod_song_list {
            position: relative;
            color: #fff;
            padding-bottom: 50px;

            a {
                display: flex;
                height: 60px;
                padding-left: 10px;
                color: #fff;

                .song_list_no {
                    display: flex;
                    width: 30px;
                    margin-right: 10px;
                    font-size: 18px;
                    justify-content: center;
                    align-items: center;
                }

                .song_list_body {
                    position: relative;
                    flex: 1;
                    padding-right: 15px;
                    display: flex;
                    flex-direction: column;
                    justify-content: center;

                    h3 {
                        line-height: 22px;
                        font-size: 18px;
                        font-weight: 400;
                        overflow: hidden;
                        white-space: nowrap;
                        text-overflow: ellipsis;
                        margin-bottom: 8px;
                    }

                    p {
                        font-size: 14px;
                        overflow: hidden;
                        white-space: nowrap;
                        text-overflow: ellipsis;
                        color: rgba(255, 255, 255, .6);
                    }
                }
            }
        }
    }

    .mint-indicator-mask {
        opacity: 1;
        background-color: rgba(0, 0, 0, .5);
    }
    .mint-indicator-wrapper {
        z-index: 999;
    }
</style>